import { Toast } from 'antd-mobile';

export const formatDate = date => {
    const pad = n => (n < 10 ? `0${n}` : n);
    const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}`;
    return `${dateStr}`;
};

/**
 * 检查Form表单错误提示
 * @param {Array} keyList 需要校验字段
 * @param {string} error form表单返回的错误信息
 */
export function validateForm(keyList = [], error) {
    const showToast = (list, len) => {
        for (let i = 0; i < len; i += 1) {
            if (error[list[i]]) {
                Toast.info(error[list[i]].errors[0].message);
                return false;
            }
        }
        return '';
    };
    // 当有指定项检查的时候
    if (keyList.length > 0) {
        showToast(keyList, keyList.length);
    } else {
        showToast(Object.keys(error), Object.keys(error).length);
    }

    return true;
}
